﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

#region Country
namespace backoffice.Data
{
    partial class Country
    {
        public void setCountry(Country c)
        {
            this.CountryID = c.CountryID;
            this.Name = c.Name;
        }
    }
}
#endregion

#region dataCountry
namespace backoffice.Data
{
    public class dataCountry
    {
        #region Create
        public static void createCountry(Country c)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                c.CountryID = Guid.NewGuid();

                db.Countries.InsertOnSubmit(c);
                db.SubmitChanges(); 
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Get
        public static Country getCountry(Guid guid)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Countries.FirstOrDefault(c => c.CountryID == guid);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Country> getAllCountries()
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Countries;
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Set
        public static Boolean changeCountry(Country c)
        {
            try
            {
                if (c == null) return false;
                else
                {
                    db2growDataContext db = new db2growDataContext();

                    Country res = db.Countries.FirstOrDefault(co => co.CountryID == c.CountryID);
                    if (res == null) return false;
                    else
                    {
                        res.setCountry(c);
                        db.SubmitChanges();
                        return true;
                    }
                }

            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Delete
        #endregion
    }
}
#endregion